package com.jjwxc.jwjskandriod.my.activity

import android.graphics.Typeface
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.viewpager.widget.ViewPager
import com.alibaba.fastjson.JSON
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import com.google.android.material.tabs.TabLayout.TabLayoutOnPageChangeListener
import com.jjwxc.jwjskandriod.R
import com.jjwxc.jwjskandriod.config.BaseActivity
import com.jjwxc.jwjskandriod.config.Constants
import com.jjwxc.jwjskandriod.config.Url
import com.jjwxc.jwjskandriod.framework.http.FFNetWork
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack
import com.jjwxc.jwjskandriod.model.VipDistanceResponse
import com.jjwxc.jwjskandriod.model.request.UserProfileRequest
import com.jjwxc.jwjskandriod.my.adapter.SelectAvatarAdapter
import com.jjwxc.jwjskandriod.widget.ReadBuyVipPop

class SelectAvatarActivity : BaseActivity() {
    /**
     * 菜單標題
     */
    private val TAB_TITLES by lazy {
        arrayOf(getString(R.string.portrait), getString(R.string.ornament))
    }

    private var viewPager: ViewPager? = null
    private var tabLayout: TabLayout? = null
    var userIsVip = false

    /**
     * 頁卡適配器
     */
    private var adapter: SelectAvatarAdapter? = null

    var profileImgUrl: String? = null;
    var backgroundBox: String? = null;

    override fun getContentViewId(): Int = R.layout.activity_select_avatar

    override fun findView() {
        viewPager = findViewById(R.id.vpHead)
        tabLayout = findViewById(R.id.tabHead)
    }

    override fun setListener() {
        title = getString(R.string.modify_your_avatar_and_decorations)
    }

    override fun afterCreate() {
        profileImgUrl = intent.getStringExtra(Constants.ProfileImgUrl)
        backgroundBox = intent.getStringExtra(Constants.backgroundBox)

        setTabs(tabLayout!!, layoutInflater, TAB_TITLES)
        initPager()
    }

    /**
     * 設置頁卡顯示效果
     *
     * @param tabLayout
     * @param inflater
     * @param tabTitlees
     */
    private fun setTabs(tabLayout: TabLayout, inflater: LayoutInflater, tabTitlees: Array<String>) {
        for (i in tabTitlees.indices) {
            val tab = tabLayout.newTab()
            val view = inflater.inflate(R.layout.bookshelf_item_menu, null)
            // 使用自定義視圖，目的是為了便於修改，也可使用自帶的視圖
            tab.customView = view
            val tvTitle = view.findViewById<View>(R.id.txt_bookshelf_tab) as TextView
            tvTitle.text = tabTitlees[i]
            tabLayout.addTab(tab)
        }
    }

    private fun initPager() {
        adapter = SelectAvatarAdapter(supportFragmentManager)
        viewPager!!.adapter = adapter
        val linearLayout = tabLayout!!.getTabAt(0)!!.customView as LinearLayout?
        val textView = linearLayout!!.getChildAt(0) as TextView
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15f)
        textView.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
        // 關聯切換
        viewPager!!.addOnPageChangeListener(TabLayoutOnPageChangeListener(tabLayout))
        tabLayout!!.addOnTabSelectedListener(object : OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {
                // 取消平滑切換
                viewPager!!.setCurrentItem(tab.position, true)
                val s = tabLayout!!.tabCount
                for (i in 0 until s) {
                    if (tab.position == i) {
                        val linearLayout = tabLayout!!.getTabAt(i)!!.customView as LinearLayout?
                        val textView = linearLayout!!.getChildAt(0) as TextView
                        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15f)
                        textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD))
                    } else {
                        val linearLayout = tabLayout!!.getTabAt(i)!!.customView as LinearLayout?
                        val textView = linearLayout!!.getChildAt(0) as TextView
                        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12f)
                        textView.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
                    }
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab) {}
            override fun onTabReselected(tab: TabLayout.Tab) {}
        })
    }

    /**
     * 獲取Vip信息
     */
    fun getVipDistance() {
        FFNetWork().post(
            Url.vipDistance,
            null,
            object : FFNetWorkCallBack<VipDistanceResponse>() {
                override fun onSuccess(response: VipDistanceResponse) {
                    if (response.code == 200) {
                        userIsVip = response.data <= 0
                    }
                }

                override fun onFail(response: VipDistanceResponse): Boolean {
                    return false
                }
            },
            VipDistanceResponse::class.java
        )
    }

    /**
     * 更新用戶信息
     */
    fun updateUserProfile(request: UserProfileRequest) {
        post(Url.updateUserProfile, "",
            String::class.java, object : FFNetWorkCallBack<String>() {
                override fun onSuccess(response: String) {
                    if (response.contains("200")) {
                        /*tv_nickName.setText("暱稱：" + et_views.getText().toString())
                        popWindow.dismiss()*/
                    }
                }

                override fun onFail(response: String): Boolean {
                    return false
                }
            }, JSON.toJSONString(request)
        )
    }

    /**
     * VIP專享體驗信息提示
     */
    fun toastVipInfo(view: View) {
        ReadBuyVipPop().readBuyVipWindow(view, this, null, -1)
    }
}